<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>
<%@ page import="dao.DaoException" %>
<%@ page import="dao.productDao.ProductDaoImpl" %>
<%@ page import="model.Product" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<html>
<head>
    <title>显示商品详情信息</title>

    <script language="JavaScript" type="text/javascript">
        function check(form) {
            const regu = /^[1-9]\d*$/;
            if (form.quantity.value === '') {
                alert("数量值不能为空！");
                form.quantity.focus();
                return false;
            }
            if (!regu.test(form.quantity.value)) {
                alert("必须输入整数！");
                form.quantity.focus();
                return false;
            }
        }
    </script>

    <c:if test="${msg != null}">
        <script>
            alert("${msg}");
        </script>
    </c:if>
</head>
<body>
<%
    int pid = Integer.parseInt(request.getParameter("id"));

    ProductDaoImpl dao = new ProductDaoImpl();
    Product product;
    try {
        product = dao.findById(pid);
%>
<p>商品详细信息</p>
<div style="float: left; margin-right: 10px">
    <img style="width: 130px" src=<%=product.getImgURL()%>>
</div>
<div style="float: left">
    <form name="my-form" method="post" action="addToCart">
        <input type="hidden" name="id" value='<%=product.getPid()%>'>
        <table>
            <tr>
                <td>
                    商品名:
                </td>
                <td>
                    <%=product.getPname()%>
                </td>
            </tr>
            <tr>
                <td>
                    价格:
                </td>
                <td>
                    <%=product.getPrice()%>
                </td>
            </tr>
            <tr>
                <td>
                    库存量:
                </td>
                <td>
                    <%=product.getStock()%>
                </td>
            </tr>
            <tr>
                <td>
                    类型:
                </td>
                <td>
                    <%=product.getType()%>
                </td>
            </tr>
            <tr>
                <td>
                    <input type="text" name="quantity" id="quantity">
                </td>
                <td>
                    <input
                            type="image"
                            border="0"
                            src="images/gw.jpg"
                            width="95"
                            align="bottom"
                            onclick="return check(this.form)"
                    />
                </td>
            </tr>
        </table>
    </form>
</div>

</body>
</html>
<%
    } catch (DaoException e) {
        e.printStackTrace();
    }
%>
